<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<link rel="stylesheet" href="../css/simple.css" />
<title>面向对象的编程</title>
</head>
<body>
<h2>创建对象</h2>
<p class="ti2em">之前有通过构造函数 var o = new Object() 与 var o = {} 但代码重复量大</p>
<h4>工厂模式创建对象</h4>
<button onclick="create()">使用函数创建对象（工厂模式）console里</button>
 <pre>
    如：
      function createPerson(name,age,job){
        var o = new Object();
        o.name = name;
        o.age = age;
        o.job = job;
        o.sayName = function(){
          alert(this.name);
        }
        return o;
      }
      var person1 = createPerson("nicholas",27,"enginner");
      var person2 = createPerson("greg",37,"doctor");
      console.log(person1);
      console.log(person2);
 </pre>
 <script type="text/javascript">
 function create(){
    function createPerson(name,age,job){
      var o = new Object();
      o.name = name;
      o.age = age;
      o.job = job;
      o.sayName = function(){
        alert(this.name);
      };
      return o;
    }
    var person1 = createPerson("nicholas",27,"enginner");
    var person2 = createPerson("greg",37,"doctor");
    console.log(person1);
    console.log(person2);
 }
 </script>
 <h4>使用构造函数的模式</h4>
 <button onclick="constructorFun()">使用构造函数的模式  console里</button>
 <pre>
    如：
      function Person(name,age,job){
        this.name = name;
        this.age = age;
        this.job = job;
        this.sayName = function(){
          alert(this.name);
        }
      }
      var person1 = new Person("nicholas",28,"enginner");
      var person2 = new Person("greg",38,"doctor")
 </pre>
 <script type="text/javascript">
  function constructorFun(){
    function Person(name,age,job){
      console.log(this);
      this.name = name;
      this.age = age;
      this.job = job;
      this.sayName = function(){
        alert(this.name);
      }
    }
    var person1 = new Person("nicholas",28,"enginner");
    var person2 = new Person("greg",38,"doctor");
    console.log(person1);console.log(person2);
  }
</script>
<p>利用构造函数创建对象</p>
<p class="ti2em">1.没有显试的创建对象，没有return语句</p>
<p class="ti2em">2.直接将属性和方法赋值给this对象</p>
<p class="ti2em">构造函数的函数名首字母一般大写</p>
<p>要创建Person的新实例，必须使用new操作符。经历了四个过程</p>
<p class="ti2em">创建一个新的对象</p>
<p class="ti2em">将构造函数的作用域赋值给新对象（因此this指向这个新对象）</p>
<p class="ti2em">执行构造函数中的代码（为这个新对象添加属性）</p>
<p class="ti2em">返回新对象</p>
<p>对象有一个constructor属性，对象的构造函数</p>
<pre>
  alert(person1.constructor == Person)   // true
  alert(person2.constructor == Person)   // true

  alert(person1 instanceof Object)  //true
  alert(person1 instanceof Person)  //true
</pre>
<p>以这种方式定义的构造函数在window对象中</p>

<h4>构造函数当作函数</h4>
<p>构造函数也是函数与普通函数一样，只有通过new操作符来调用时，他就可以作为构造函数</p>
<button onclick="constructorAsFun()">构造函数当作函数</button>
<pre>
  function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function(){
      alert(this.name)
    }
  }




  //1当作构造函数来用
  var person = new Person("nicholas",27,"enginner");
  person.sayName();

  //2当作普通函数用
  Person("greg",38,"doctor");
  window.sayName();   // 当直接调用Person()函数时函数的作用域是 window

  //3绑定在另一个对象中
  var o = new Object();
  Person.call(o,"kristen",40,"nurse");  //  此处将Person()作用域绑定到了o对象上，然后在执行Person()函数
  o.sayName();
</pre>


<script type="text/javascript" src="test.js"></script>
</body>
</html>
